package llama.view.panels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import llama.core.Contact;
import llama.ctrl.AssignTagsCtrl;
import llama.ctrl.ControllerFactory;
import llama.ctrl.ConversationCtrl;
import llama.ctrl.ConversationCtrl.ConversationOption;
import llama.ctrl.RemoveContactCtrl;
import llama.ctrl.ShowContactInfoCtrl;

public class ContactMenu {
	private final JPopupMenu popupMenu = new JPopupMenu();
	private final JMenuItem conversation = new JMenuItem("<html><b>Start conversation</b></html>");
	private final JMenuItem info = new JMenuItem("Show contact information");
	private final JMenu setTag = new JMenu("Set tag");
	private final JMenuItem[] tags;
	private final JMenuItem remove = new JMenuItem("Remove contact");
	
	private Contact contact;
	
	public ContactMenu(final Contact contact) {
		this.contact = contact;
		
		popupMenu.add(conversation);
		popupMenu.add(info);
		popupMenu.add(setTag);
		popupMenu.addSeparator();
		popupMenu.add(remove);
		
		conversation.addActionListener(new PopupItemListener());
		info.addActionListener(new PopupItemListener());
		remove.addActionListener(new PopupItemListener());
		
		conversation.setMnemonic(KeyEvent.VK_S);
		info.setMnemonic(KeyEvent.VK_C);
		remove.setMnemonic(KeyEvent.VK_D);
		
		tags = new JMenuItem[llama.constants.DefaultTags.TAGS.length];
		
		for (int i = 0; i < tags.length; i++) {
			tags[i] = new JMenuItem(llama.constants.DefaultTags.TAGS[i]);
			tags[i].setActionCommand(llama.constants.DefaultTags.TAGS[i]);
			tags[i].addActionListener(new PopupItemListener());
			setTag.add(tags[i]);
		}
		
        //Disable "assign tag"-option for the tag that the clicked user already has
        for (int i = 0; i < tags.length; i++) {
                if(tags[i].getActionCommand().equals(contact.getTag())){
                        tags[i].setEnabled(false);
                }
        }

	}
	
	Contact getContactMenuContact() {
		return this.contact;
	}
	
	/**
     * Displays the popup menu at the position x,y in the coordinate
     * space of the component invoker.
     *
     * @param invoker the component in whose space the popup menu is to appear
     * @param x the x coordinate in invoker's coordinate space at which 
     * the popup menu is to be displayed
     * @param y the y coordinate in invoker's coordinate space at which 
     * the popup menu is to be displayed
     */
	public void show(Component invoker, int x, int y) {
		popupMenu.show(invoker, x, y);
	}
	
	private class PopupItemListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(getContactMenuContact() != null) {
				Contact contact = getContactMenuContact();
				
				if(e.getSource() == conversation) {
					ConversationCtrl conversationCtrl = (ConversationCtrl) ControllerFactory.getController(ConversationCtrl.class);
					conversationCtrl.setSelectedContact(ConversationOption.SELECT, contact.getUser());
					conversationCtrl.execute();
				}
				else if(e.getSource() == info) {
					ShowContactInfoCtrl showContactInfoCtrl = (ShowContactInfoCtrl) ControllerFactory.getController(ShowContactInfoCtrl.class);
					showContactInfoCtrl.setContact(contact);
					showContactInfoCtrl.execute();
				}
				else if(e.getSource() == remove) {
					RemoveContactCtrl removeContact = (RemoveContactCtrl) ControllerFactory.getController(RemoveContactCtrl.class);
					removeContact.setUser(contact.getUser());
					removeContact.execute();
				}
				else {					
					for (int i = 0; i < tags.length; i++) {
						if(e.getActionCommand().equals(tags[i].getActionCommand())){							
							AssignTagsCtrl tag = (AssignTagsCtrl) ControllerFactory.getController(AssignTagsCtrl.class);
							tag.assignTag(contact.getUser(), tags[i].getActionCommand());
							tag.execute();
							break;
						}
					}
				}
			}
	    }
	}
}
